﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SOEN341_SCHEDULER_PHASE1
{
    public partial class ui_form_SchedulerViewer : Form
    {
        const int LOWEST_VALUE_HOUR = 8;
        const int MAX_VALUE_HOUR = 22;
        const int LOWEST_VALUE_MINUTE = 0;
        const int MAX_VALUE_MINUTE = 60;
        const int INTERVAL_VALUE_MINUTE = 15;
        const int BLOCKS_PER_HOUR = MAX_VALUE_MINUTE / INTERVAL_VALUE_MINUTE;
        Font _defaultFont = new Font(FontFamily.GenericSansSerif, 6);
        
        public ui_form_SchedulerViewer(bool showLarge)
        {
            InitializeComponent();

            this.AutoSize = true;
            //ui_tableLayout.AutoSize = true;
            //InitializeGenerateDefaultLabels();

            if (showLarge)
            {
                DisplayLargerView(GlobalVariablesClass.courseSelected);
            }
        }

        void DisplayLargerView(List<Course> cSel)
        {
            Font tableDefaultFont = new Font(FontFamily.GenericSansSerif, 6);
            TableGridPublicStaticMethods ttgb = new TableGridPublicStaticMethods();
            ttgb.defaultTableGrid = ui_tableLayout;
            ttgb.TableGridInitializeGenerateDefaultLabels(tableDefaultFont);

            ttgb.Clear();
            Course toAdd = null;
            Course theTutorial = null;
            Course theLab = null;
            bool isCollide = false;
            //get the courses
            Course[] arrCourse = GlobalVariablesClass.courseSelected.ToArray();

            for (int i = 0; i < arrCourse.Length; i++)
            {
                toAdd = arrCourse[i];
                theTutorial = toAdd.GetTutorial();
                theLab = toAdd.GetLab();

                if (theTutorial != null)
                {
                    ttgb.AddCourseToGrid(theTutorial);
                }
                if (theLab != null)
                {
                    ttgb.AddCourseToGrid(theLab);
                }

                ttgb.AddCourseToGrid(toAdd);
            }

        }

        // public ui_form_SchedulerViewer(List<Course> courses)
        //{
        //   AddMultipleCoursesToGrid(courses);
        //}

        private void ScheduleViewer_Load(object sender, EventArgs e)
        {
            //AddMultipleCoursesToGrid(GlobalVariablesClass._gridToEnlarge);
            //Uncomment this to see what is the display
            //CustomDateTime cdt = new CustomDateTime(19, 15, 20, 30, DayOfWeek.Monday);
            //AddGridBlock("COMP346", "WW", "H400", cdt);
            
        }

        private void AddSingleCourseToGrid(Course newCourse)
        {
            AddGridBlock(newCourse.GetName(), newCourse.GetSection(), "N/A", newCourse.GetTimeTableBlock().GetCustomDateTime());
        }

        void AddMultipleCoursesToGrid(List<Course> newCourses)
        {
            Course[] toAdd= newCourses.ToArray();
            for (int i = 0; i < toAdd.Length; i++)
            {
                AddGridBlock(toAdd[i].GetName(), toAdd[i].GetSection(), "room#", toAdd[i].GetTimeTableBlock().GetCustomDateTime());
                if (toAdd[i].GetLab() != null)
                {
                    MessageBox.Show(toAdd[i].GetLab().GetName());
                    AddGridBlock(toAdd[i].GetLab().GetName(), toAdd[i].GetLab().GetSection(), "room#", toAdd[i].GetLab().GetTimeTableBlock().GetCustomDateTime());
                }
            }
        }

        private void AddGridBlock(string courseName, string courseSection, string courseRoom, CustomDateTime custDateTime)
        {
            int startRowNumber = GetRowNumber(custDateTime, true);
            int endRowNumber = GetRowNumber(custDateTime, false);
            int rowSpan = endRowNumber - startRowNumber;
            List<DayOfWeek> daysOfWeek = custDateTime.GetDayOfWeek();
            DayOfWeek[] arrDayOfWeek = daysOfWeek.ToArray();
            //int dayOfWeek=(int)custDateTime.GetDayOfWeek();

            //MessageBox.Show(startRowNumber.ToString() + " || " + endRowNumber.ToString());
            Label[] ui_lbl_toAdd = new Label[arrDayOfWeek.Length];

            for (int i = 0; i < arrDayOfWeek.Length; i++)
            {
                ui_lbl_toAdd[i] = new Label();
                ui_lbl_toAdd[i].AutoSize = true;
                ui_lbl_toAdd[i].BackColor = Color.Red;
                ui_lbl_toAdd[i].Dock = DockStyle.Fill;
                ui_lbl_toAdd[i].Font = _defaultFont;
                ui_lbl_toAdd[i].TextAlign = ContentAlignment.MiddleCenter;
                ui_lbl_toAdd[i].Text = courseName + " " + courseSection + "\n" + courseRoom + "\n" +
                    custDateTime.GetTimeInterval();

                int dayOfThis = (int)arrDayOfWeek[i];

                ui_tableLayout.Controls.Add(ui_lbl_toAdd[i], dayOfThis, startRowNumber);
                ui_tableLayout.SetRowSpan(ui_lbl_toAdd[i], rowSpan);
            }
            
        }
       

        /// <summary>
        /// returns row number
        /// </summary>
        /// <param name="cdt">custome date time</param>
        /// <param name="getStart">TRUE: return start time row # 
        /// FALSE: return end time row #</param>
        /// <returns></returns>
        private int GetRowNumber(CustomDateTime cdt, bool getStart)
        {
            int hour;
            int minute;
            int row;

            //constraints

            if (getStart)
            {
                //get hour first
                hour = cdt.GetStartHour();
                minute = cdt.GetStartMinute();
            }
            else
            {
                hour = cdt.GetEndHour();
                minute = cdt.GetEndMinute();
            }

            //set hour to be at the FIRST block of that hour time
            row = hour - LOWEST_VALUE_HOUR;
            row *= BLOCKS_PER_HOUR;
            row++;
            
            //for minute
            row += minute / INTERVAL_VALUE_MINUTE;
            return row;
        }

        private void InitializeGenerateDefaultLabels()
        {
            //time column label
            Label ui_lbl_grid_time_column = new Label();
            ui_lbl_grid_time_column.Text = "Time";
            ui_lbl_grid_time_column.TextAlign = ContentAlignment.MiddleCenter;
            ui_lbl_grid_time_column.Font = _defaultFont;
            ui_tableLayout.Controls.Add(ui_lbl_grid_time_column, 0, 0);

            //day labels
            Label[] ui_lbl_grid_day = new Label[5];
            
            for (int i = 0; i < ui_lbl_grid_day.Length; i++)
            {
                ui_lbl_grid_day[i] = new Label();
                ui_lbl_grid_day[i].TextAlign = ContentAlignment.MiddleCenter;
                ui_lbl_grid_day[i].Font = _defaultFont;
            }
            
            ui_lbl_grid_day[0].Text = "Monday";
            ui_lbl_grid_day[1].Text = "Tuesday";
            ui_lbl_grid_day[2].Text = "Wednesday";
            ui_lbl_grid_day[3].Text = "Thursday";
            ui_lbl_grid_day[4].Text = "Friday";

            for (int i = 0; i < ui_lbl_grid_day.Length; i++)
            {
                ui_tableLayout.Controls.Add(ui_lbl_grid_day[i], i+1,0);
            }

            //time labels
            Label[] ui_lbl_grid_time = new Label[57];

            //for the interval calculations
            int currentTimeMinutes = 0;
            int currentTimeHours=LOWEST_VALUE_HOUR;

            for (int i = 0; i < ui_lbl_grid_time.Length; i++)
            {
                ui_lbl_grid_time[i] = new Label();

                //smaller font
                ui_lbl_grid_time[i].Font = _defaultFont;

                //go by 15 min interval
                if (currentTimeMinutes < 10)
                {
                    ui_lbl_grid_time[i].Text = currentTimeHours + ":0" + currentTimeMinutes;
                }
                else
                {
                    ui_lbl_grid_time[i].Text = currentTimeHours + ":" + currentTimeMinutes;
                }
                currentTimeMinutes += INTERVAL_VALUE_MINUTE;
                if (currentTimeMinutes == MAX_VALUE_MINUTE)
                {
                    currentTimeMinutes = LOWEST_VALUE_MINUTE;
                    currentTimeHours++;
                }
                ui_tableLayout.Controls.Add(ui_lbl_grid_time[i], 0, i + 1);
            }

        }

        private void ui_tableLayout_DragEnter(object sender, DragEventArgs e)
        {
            
        }

        private void ui_tableLayout_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
